<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\common\library\wechat;

use app\common\library\helper;
use cores\exception\BaseException;

/**
 * 微信小程序用户管理类
 * Class WxUser
 * @package app\common\library\wechat
 */
class WxJsapi extends WxBase
{
     public function getJsapi($url){
         $accessToken = $this->getAccessToken();
         $jsApiTicket = $this->getJsApiTicket($accessToken);
         // 生成签名
         $timestamp = time();
         $nonceStr = $this->generateNonceStr(32);
         $string1 = "jsapi_ticket=$jsApiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
         $signature = sha1($string1);
         // 返回配置信息给前端
         $configData = array(
             'appId' => $this->appId,
             'timestamp' => $timestamp,
             'nonceStr' => $nonceStr,
             'signature' => $signature
         );
         return $configData;
     }

// 生成一个随机的 nonceStr
     public function generateNonceStr($length = 16) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }



}